
export default {
  name:"AddressManage",
  components:{},
  data(){
    return {
      addList:[],
      pageNo:1,
      rows:6,
      hasNext:false,
    }
  },
  methods:{
    androidCallJs(val) {

    },
    initData(type){
      this.$ajax.get("address/list",(res) => {
        if(res.code=='200'){
          for(var i=0;i<res.data.length;i++){
            if(res.data[i].type==0){
              res.data[i].checked = true;
            }
          }
          if(type==0){
            this.addList = res.data;
          }else{
            this.addList = this.addList.concat(res.data);
          }
          type==0&&this.$refs.scroll&&this.$refs.scroll.finishPullToRefresh();
           if(res.data&&res.data.length < this.rows||res.data==undefined){
          // if(!res.data||res.data.length < this.rows){
            this.hasNext = false;
            type==1&&this.$refs.scroll&&this.$refs.scroll.finishInfinite(true);
          }else{
            this.hasNext = true;
            type==1&&this.$refs.scroll&&this.$refs.scroll.finishInfinite(false);
          }
        }else{
          this.$vux.toast.text(res.msg, 'center');
        }

      },{pageNo :this.pageNo,pageSize:this.rows});
    },
    setDefault(item){
      for(var i=0;i<this.addList.length;i++){
        this.addList[i].type = 9;
        if(this.addList[i].id==item.id){
          item.checked = true;
          item.type = 0;
          this.$ajax.post("address/set/default",{addressId:item.id},(res) => {
            console.log(res)
            if(res.code==200){
              this.$vux.toast.text('设置默认', 'center');
            }else{
              this.$vux.toast.text(res.msg, 'center');
            }
          });
          // break;
        }else{
          this.addList[i].checked = false;
        }
      }
    },
    modifyAddress(item){
      if(this.$fromApp(this)){
        this.$callJava({url:this.$webUrl+"address/add?from=app",item:item,title:"修改地址",theme:1})
      }else{
        this.$router.push({ path:'/address/add',query:{item:item}})
      }
    },
    refresh(){
      this.hasNext = true;
      this.pageNo = 1;
      this.initData(0);
    },
    infinite(){
      if(this.hasNext){
        this.pageNo ++;
        this.initData(1);
      }else{
        this.$refs.scroll&&this.$refs.scroll.finishInfinite(true)
      }
    },
  },
  mounted:function(){
    this.initData(0);
    window['androidCallJs'] = this.androidCallJs;
  }
}
